package com.smartalarmclock.UI;

import java.util.ArrayList;
import java.util.HashMap;

import com.smartalarmclock.Logic.AcManager;
import com.smartalarmclock.Model.IProfile;
import com.smartalarmclock.Model.ITimeFrame;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;


public class ScheduleForProfileActivity extends Activity {
	
	IProfile m_Profile = null;
	private int m_ProfileId;
	@Override
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_schedule_screen);
        Bundle bundle = this.getIntent().getExtras();
        m_ProfileId = bundle.getInt(Messages.PROFILE_DETAILS_PROFILE_ID);
    	m_Profile = AcManager.GetInstance().GetProfileDetails(this, m_ProfileId);
        final TextView scheduleActivityTitle = (TextView)findViewById(R.id.textView_schedule_title);
        
        scheduleActivityTitle.setText("Scheduling ranges for " + m_Profile.GetName());
        
        refreshScheduleList();
    }
        
    @Override
    protected void onResume()
    {
    	Log.v("Schedule", "Now entering Schedule onResume");
    	refreshScheduleList();
    	super.onResume();
    }
    
    private void refreshScheduleList()
    {
    	Log.v("Schedule", "Now entering refreshScheduleList");
    	
    	// set schedule list adapter and items on-click listener
    	final ListView scheduleListView = (ListView)findViewById(R.id.listView_schedule_items);
		scheduleListView.setAdapter(new ScheduleArrayAdapter(this, m_Profile));
    }

	private static ArrayList<ITimeFrame> makeFakeList(Context context, IProfile profile)
	{
		ArrayList<ITimeFrame> fakeList = AcManager.GetInstance().GetTimeFramesForProfile(context, profile);
		fakeList.add(null);
		return fakeList;
	}
    
    private class ScheduleArrayAdapter extends ArrayAdapter<ITimeFrame>
    {
    	private Context m_Context = null;
    	HashMap<ITimeFrame, Integer> m_IdMap = null;
    	
    	public ScheduleArrayAdapter(Context context, IProfile profile)
    	{
    		super(context, R.layout.main_menu_item_details_view, makeFakeList(context, profile));
    		m_Context = context;
    		
    		updateScheduleData();
    	}
    	
    	public View getView(int position, View convertView, ViewGroup parent)
    	{
    		ViewHolder holder;
    		
    		if (null == convertView)
    			if (null == getItem(position))
    			{
    				convertView = LayoutInflater.from(m_Context).inflate(R.layout.schedule_range_item, null);
    				holder = new ViewHolder();
    				holder.ButtonFromDate = (Button)convertView.findViewById(R.id.button_schedule_item_from);
    				holder.ButtonToDate = (Button)convertView.findViewById(R.id.button_schedule_item_to);
    				holder.ButtonDelete = (ImageView)convertView.findViewById(R.id.imageButton_schedule_item_delete);
    			
    				convertView.setTag(holder);
    				holder.ButtonFromDate.setText("--");
    				holder.ButtonToDate.setText("--");
    			}
    			else
    			{
    				convertView = LayoutInflater.from(m_Context).inflate(R.layout.schedule_range_item, null);
    				holder = new ViewHolder();
    				holder.ButtonFromDate = (Button)convertView.findViewById(R.id.button_schedule_item_from);
    				holder.ButtonToDate = (Button)convertView.findViewById(R.id.button_schedule_item_to);
    				holder.ButtonDelete = (ImageView)convertView.findViewById(R.id.imageButton_schedule_item_delete);

    				convertView.setTag(holder);
    			
    				holder.ButtonFromDate.setText(getItem(position).GetStartTime().toString());
    				holder.ButtonToDate.setText(getItem(position).GetEndTime().toString());
    		} else {
    			holder = (ViewHolder) convertView.getTag();
    			
    			holder.ButtonFromDate.setText(getItem(position).GetStartTime().toString());
        		holder.ButtonToDate.setText(getItem(position).GetEndTime().toString());
    		}

    		return convertView;
    	}
    	
    	private void updateScheduleData()
    	{
    		ArrayList<ITimeFrame> scheduleList = AcManager.GetInstance().GetTimeFramesForProfile(m_Context, m_Profile);
    		m_IdMap = new HashMap<ITimeFrame, Integer>();
    		// set profiles in id-map
    		int i = 0;
    		for (ITimeFrame timeFrameItem : scheduleList)
    		{
    			m_IdMap.put(timeFrameItem, i++);
    		}
    		
    		// add another fake one
    		m_IdMap.put(null,  -1);
    	}
    	
    	@Override
    	public long getItemId(int position)
    	{
    		ITimeFrame item = getItem(position);
    		return m_IdMap.get(item);
    	}
    	
    	@Override
    	public boolean hasStableIds()
    	{
    		return true;
    	}
    	
    	@Override
    	public void notifyDataSetChanged()
    	{
    		updateScheduleData();
    		super.notifyDataSetChanged();
    	}
    }
    
    static class ViewHolder
    {
		Button ButtonFromDate;
		Button ButtonToDate;
		ImageView ButtonDelete;
	}

}
